package controller

import (
	myConfig "common/config"
	"common/helper"
	myUserHelp "common/userHelp"
	"encoding/json"
	"github.com/valyala/fasthttp"
	"member/model"
	"net/mail"
)

const (
	PIX_TYPE_CPF   = 0x1
	PIX_TYPE_PHONE = 0x1 << 1
	PIX_TYPE_EMAIL = 0x1 << 2
	PIX_TYPE_CNPJ  = 0x1 << 3
)

type BankcardController struct{}

type insertBankParam struct {
	PixId string `json:"pix_id"`
	//PayPassword string `json:"pay_password"`
	Realname string `json:"realname"`
	Flag     string `json:"flag"` //1 cpf 2 phone 3 email
	Account  string `json:"pix_account"`
}

// 绑定银行卡
func (that *BankcardController) Insert(ctx *fasthttp.RequestCtx) {

	param := insertBankParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	if param.PixId == "" {
		helper.Print(ctx, false, helper.BankcardIDErr)
		return
	}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	helper.InfoLog("Insert bank:%s--%s--%s--%s--%s", uid, param.Realname, param.Flag, param.Account, param.PixId)
	CfgWithdrawPixType := myConfig.GetGameCfgInt(myConfig.CfgWithdrawPixType)
	userPixType := 0
	if param.Flag == "1" {
		userPixType = PIX_TYPE_CPF
	} else if param.Flag == "2" {
		userPixType = PIX_TYPE_PHONE
	} else if param.Flag == "3" {
		userPixType = PIX_TYPE_EMAIL
	} else if param.Flag == "4" {
		userPixType = PIX_TYPE_CNPJ
	}

	if (CfgWithdrawPixType)&(userPixType) == 0 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	//增加email格式判断
	if userPixType == PIX_TYPE_EMAIL {
		_, err := mail.ParseAddress(param.Account)
		if err != nil {
			helper.Print(ctx, false, helper.EmailFMTErr)
			return
		}
	}

	Username := myUserHelp.GetMemberName(uid)
	err = model.BankcardInsert(uid, Username, param.Realname, param.Flag, param.Account, param.PixId, "PIX")
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, helper.Success)
}

func (that *BankcardController) List(ctx *fasthttp.RequestCtx) {

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	// 更新权限信息
	res, err := model.BankcardList(uid)

	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, res)
}

func (that *BankcardController) BankType(ctx *fasthttp.RequestCtx) {

	// 更新权限信息
	res, err := model.BanktypeList()
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, res)
}

type updateBankParam struct {
	PixId string `json:"pix_id"`
	State int    `json:"state" db:"state"` //0 其他,1 默认卡
}

// 修改银行卡默认状态
func (that *BankcardController) UpdateState(ctx *fasthttp.RequestCtx) {

	param := updateBankParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	//if !helper.CtypeDigit(param.PixId) {
	//	helper.Print(ctx, false, helper.BankcardIDErr)
	//	return
	//}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	err = model.BankcardUpdateState(uid, param.PixId, param.State)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, helper.Success)
}
